package com.hh.demo_ui;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.hh.listeners.OnPopupMenuItemClickListener;
import com.hh.ui.widget.UiPopupMenu;
import com.hh.ui.widget.UiPopupMenu.UIPopUpMenu_ALIGN;

public class Sample_PopupMenu extends Activity implements OnClickListener, OnPopupMenuItemClickListener {


	private UiPopupMenu _mPopupMenu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sample_popupmenu);

		Button lBtnSimple=(Button) findViewById(R.id.SPop_BtnItems);
		lBtnSimple.setOnClickListener(this);

		Button lBtnWithIcons=(Button) findViewById(R.id.SPop_BtnItemsAndDrawables);
		lBtnWithIcons.setOnClickListener(this);

		Button lBtnWithAdapter=(Button) findViewById(R.id.SPop_BtnAdapter);
		lBtnWithAdapter.setOnClickListener(this);

		Button lBtnTopLeft=(Button) findViewById(R.id.SPop_BtnTopLeft);
		lBtnTopLeft.setOnClickListener(this);
		Button lBtnTopRight=(Button) findViewById(R.id.SPop_BtnTopRight);
		lBtnTopRight.setOnClickListener(this);
		Button lBtnBottomLeft=(Button) findViewById(R.id.SPop_BtnBottomLeft);
		lBtnBottomLeft.setOnClickListener(this);
		Button lBtnBottomRight=(Button) findViewById(R.id.SPop_BtnBottomRight);
		lBtnBottomRight.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		//Simple items
		case R.id.SPop_BtnItems:
			setSimplePopupmenu(v);
			break;
			// Items Text + icon
		case R.id.SPop_BtnItemsAndDrawables:

			setPopupmenuWithIcons(v);
			break;
			// using a base adapter
		case R.id.SPop_BtnAdapter:
			setPopupmenuWithAdapter(v);
			break;

			//Button position : Top / Left
		case R.id.SPop_BtnTopLeft:
			createPopupmenu();
			_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.BOTTOM);
			_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.RIGHT);
			_mPopupMenu.show();
			break;
			// Button position : Top / Right
		case R.id.SPop_BtnTopRight:
			createPopupmenu();
			_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.BOTTOM);
			_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.LEFT);
			_mPopupMenu.show();
			break;
			// Button position : Bottom / left
		case R.id.SPop_BtnBottomLeft:
			createPopupmenu();
			_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.TOP);
			_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.RIGHT);
			_mPopupMenu.show();
			break;
			// Button position : bottom / Right
		case R.id.SPop_BtnBottomRight:
			createPopupmenu();
			_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.TOP);
			_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.LEFT);
			_mPopupMenu.show();
			break;
		default:
			break;
		}
	}


	private void createPopupmenu(){

		String[] lTabElements={"Item 1","Item 2","Item 3","Item 4"};
		_mPopupMenu=new UiPopupMenu(this, lTabElements);
		// Add items click listener
		_mPopupMenu.setOnPopupMenuItemClickListener(this);
	}
	/**
	 * How to set simple popupmenu relatively of a view (Button in our case)
	 * @param v
	 */
	private void setSimplePopupmenu(View v){

		String[] lTabElements={"Item 1","Item 2","Item 3","Item 4"};
		_mPopupMenu=new UiPopupMenu(this, lTabElements);
		// Align popupMenu on the bottom of button
		_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.BOTTOM);

		// Add items click listener
		_mPopupMenu.setOnPopupMenuItemClickListener(this);

		_mPopupMenu.show();
	}

	/**
	 * How to set popupmenu (Text and icon) relatively of a view (Button in our case)
	 * @param v
	 */
	private void setPopupmenuWithIcons(View v){

		String[] lTabElements={"Item 1","Item 2","Item 3","Item 4"};
		int[] lTabIcons={R.drawable.ic_action_user,R.drawable.ic_action_user,R.drawable.ic_action_user,R.drawable.ic_action_user};

		// get x and y postion of the button
		int[] location =new int[2];
		v.getLocationOnScreen(location);
		// Set a graphics point using coordinates
		Point p=new Point(location[0],location[1]);
	
		_mPopupMenu=new UiPopupMenu(this, p,lTabElements,lTabIcons);
		
		// add margin top (50)
		_mPopupMenu.setMarginTop(80);
		// Add items click listener
		_mPopupMenu.setOnPopupMenuItemClickListener(this);
		_mPopupMenu.show();

	}

	/**
	 * How to set popupmenu using an Adapter
	 * @param v
	 */
	private void setPopupmenuWithAdapter(View v){

		/*
		 * <=> We can build customized item by using SimpleAdapter, in this case I used an ArrrayAdapter
		 */
		ArrayAdapter<String> lAdapter=new ArrayAdapter<String>(this, R.layout.tvres,new String[]{"Elem 1","Elem 2","Elem 3","Elem 4"});
		_mPopupMenu=new UiPopupMenu(this, lAdapter);
		// Align popupMenu on the top of button
		_mPopupMenu.setAlignView(v, UIPopUpMenu_ALIGN.BOTTOM);
		// Set max showing items at 3
		_mPopupMenu.setMaxItems(3);
		// Add items click listener
		_mPopupMenu.setOnPopupMenuItemClickListener(this);
		_mPopupMenu.show();

	}

	@Override
	public void onPopupMenuItemClick(View v, int position,int id) {

		Toast.makeText(this, "Selected postion is : "+position, Toast.LENGTH_SHORT).show();

	}
}
